package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.donkingliang.groupedadapter.widget.StickyHeaderLayout;
import com.google.gson.Gson;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.NoFooterAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.ChildEntity;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.GroupEntity;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2021/3/15  10:40
 * 功 能：我的余额
 */
public class MyBalanceActivity extends BaseActivity {

    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.withdrawal)
    Button withdrawal;
    @BindView(R.id.top_up)
    Button topUp;
    @BindView(R.id.empty_image)
    ImageView empty_image;
    private RecyclerView recycler;
    private StickyHeaderLayout stickyLayout;
    private LinearLayoutManager layoutManager;
    private NoFooterAdapter adapter;
    private ArrayList<GroupEntity> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_balance;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        lists = new ArrayList<>();
        initView();
        //设置是否吸顶。
        stickyLayout.setSticky(true);

    }

    private void initAdatper() {
        layoutManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(layoutManager);
        adapter = new NoFooterAdapter(this, lists);
        adapter.setOnHeaderClickListener(new GroupedRecyclerViewAdapter.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder,
                                      int groupPosition) {
                Toast.makeText(MyBalanceActivity.this, "组头：groupPosition = " + groupPosition,
                        Toast.LENGTH_LONG).show();
                Log.e("eee", adapter.toString() + "  " + holder.toString());
            }
        });

        adapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder,
                                     int groupPosition, int childPosition) {
                Toast.makeText(MyBalanceActivity.this, "子项：groupPosition = " + groupPosition
                                + ", childPosition = " + childPosition,
                        Toast.LENGTH_LONG).show();
            }
        });
        recycler.setAdapter(adapter);
    }

    private void initView() {

        recycler = (RecyclerView) findViewById(R.id.rv_list);
        stickyLayout = (StickyHeaderLayout) findViewById(R.id.sticky_layout);

    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        lists.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONObject data = object.getJSONObject("data");
                        JSONArray list = data.getJSONArray("list");
                        money.setText(data.getString("money"));
                        Gson gson = new Gson();

                        JSONArray dates = data.getJSONArray("dates");
                        for (int i = 0; i < dates.length(); i++) {

                            ArrayList<ChildEntity> children = new ArrayList<>();
                            JSONObject jb = dates.getJSONObject(i);
                            GroupEntity ge = gson.fromJson(jb.toString(), GroupEntity.class);
                            for (int j = 0; j < list.length(); j++) {

                                if (jb.getString("date").equals(list.getJSONObject(j).getString("date"))) {

                                    children.add(gson.fromJson(list.getJSONObject(j).toString(), ChildEntity.class));

                                }
                                ge.setChildren(children);
                            }

                            lists.add(ge);

                        }

                        if (lists.size()>0){

                            empty_image.setVisibility(View.GONE);

                        }else{
                            empty_image.setVisibility(View.VISIBLE);
                        }

                        initAdatper();
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("一次", "异常" + e.toString());
                    }

                    break;

            }

        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        getBill();
    }

    public void getBill() {

        List<String> ls = new ArrayList<>();
        ls.add("10");
        ls.add("1");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getBills");
        params.put("data[row]", 1 + "");
        params.put("data[number]", 10 + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.member/entry", params, handler, 1, 404);


    }

    @OnClick({R.id.withdrawal, R.id.top_up})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.withdrawal:

                Toast("暂时没法提现！");

                break;
            case R.id.top_up:
                startActivity(TopUpActivity.class);
                break;
        }
    }
}
